package net.lab1024.sa.admin.module.business.erp.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import net.lab1024.sa.admin.module.business.erp.dao.AccountDao;
import net.lab1024.sa.admin.module.business.erp.dao.CalendarsaDao;
import net.lab1024.sa.admin.module.business.erp.dao.NmmordDao;
import net.lab1024.sa.admin.module.business.erp.dao.NmmordaDao;
import net.lab1024.sa.admin.module.business.erp.domain.entity.*;
import net.lab1024.sa.admin.module.business.erp.domain.form.add.NmmordaAddForm;
import net.lab1024.sa.admin.module.business.erp.domain.form.query.MmordbQueryForm;
import net.lab1024.sa.admin.module.business.erp.domain.form.query.NmmordaQueryForm;
import net.lab1024.sa.admin.module.business.erp.domain.form.update.NmmordaUpdateForm;
import net.lab1024.sa.admin.module.business.erp.domain.vo.NmmordaVO;
import net.lab1024.sa.admin.module.system.employee.dao.EmployeeDao;
import net.lab1024.sa.admin.module.system.employee.domain.entity.EmployeeEntity;
import net.lab1024.sa.base.common.code.UserErrorCode;
import net.lab1024.sa.base.common.domain.PageResult;
import net.lab1024.sa.base.common.domain.ResponseDTO;
import net.lab1024.sa.base.common.util.SmartBeanUtil;
import net.lab1024.sa.base.common.util.SmartPageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

/**
 * 生产订单与用户关系表 Service
 *
 * @Author AKITA
 * @Date 2025-04-19 13:01:26
 * @Copyright AKITA
 */

@Service
public class NmmordaService {

    @Resource
    private NmmordaDao nmmordaDao;
    @Resource
    private CalendarsaDao calendarsaDao;
    @Resource
    private NmmordDao nmmordDao;
    @Resource
    private AccountDao accountDao;


    public PageResult<NmmordaVO> queryPage(NmmordaQueryForm queryForm) {
        Page<?> page = SmartPageUtil.convert2PageQuery(queryForm);
        List<NmmordaVO> list = nmmordaDao.queryPage(page, queryForm);
        return SmartPageUtil.convert2PageResult(page, list);
    }

    public ResponseDTO<String> add(NmmordaAddForm addForm) {
        NmmordaEntity nmmordaEntity = SmartBeanUtil.copy(addForm, NmmordaEntity.class);

        List<NmmordaEntity> nmmordaEntities = nmmordaDao.selectList(new LambdaQueryWrapper<NmmordaEntity>().eq(NmmordaEntity::getDocEntry, addForm.getDocEntry()).eq(NmmordaEntity::getUserId, addForm.getUserId()).eq(NmmordaEntity::getNowDate, addForm.getNowDate()));
        if (!nmmordaEntities.isEmpty()) {
            throw new RuntimeException("当前单据的日期重复了！");
        }

        Integer lineNum = nmmordaDao.getMaxLineNum(addForm.getDocEntry());
        NmmordEntity nmmordEntity = nmmordDao.selectById(addForm.getDocEntry());
        if (!"O".equals(nmmordEntity.getDocStatus())){
            throw new RuntimeException("单据已关闭，禁止修改！");
        }
        if (nmmordEntity.getApproved()){
            throw new RuntimeException("单据已审核，禁止修改！");
        }

        if (lineNum == null) {
            lineNum = 0;
        }
        List<CalendarsaEntity> calendarsaEntities = calendarsaDao.selectList(new LambdaQueryWrapper<CalendarsaEntity>().eq(true, CalendarsaEntity::getAbsId, addForm.getAbsId()));
        if (calendarsaEntities == null || calendarsaEntities.isEmpty()) {
            throw new RuntimeException("不存在当月日历！");
        }
        BigDecimal time1 = new BigDecimal(0);
        BigDecimal time2 = new BigDecimal(0);
        BigDecimal time3 = new BigDecimal(0);
        BigDecimal time4 = new BigDecimal(0);
        BigDecimal time5 = new BigDecimal(0);
        BigDecimal time6 = new BigDecimal(0);
        BigDecimal time7 = new BigDecimal(0);
        BigDecimal time8 = new BigDecimal(0);
        BigDecimal time9 = new BigDecimal(0);
        BigDecimal time10 = new BigDecimal(0);
        BigDecimal time11 = new BigDecimal(0);
        BigDecimal time12 = new BigDecimal(0);
        BigDecimal time13 = new BigDecimal(0);
        BigDecimal time14 = new BigDecimal(0);
        BigDecimal time15 = new BigDecimal(0);
        BigDecimal time16 = new BigDecimal(0);
        BigDecimal time17 = new BigDecimal(0);
        BigDecimal time18 = new BigDecimal(0);
        BigDecimal time19 = new BigDecimal(0);
        BigDecimal time20 = new BigDecimal(0);
        BigDecimal time21 = new BigDecimal(0);
        BigDecimal time22 = new BigDecimal(0);
        BigDecimal time23 = new BigDecimal(0);
        BigDecimal time24 = new BigDecimal(0);
        if (nmmordaEntity.getTime1()) {
            time1 = time1.add(BigDecimal.ONE);
        }
        if (nmmordaEntity.getTime2()) {
            time2 = time2.add(BigDecimal.ONE);
        }
        if (nmmordaEntity.getTime3()) {
            time3 = time3.add(BigDecimal.ONE);
        }
        if (nmmordaEntity.getTime4()) {
            time4 = time4.add(BigDecimal.ONE);
        }
        if (nmmordaEntity.getTime5()) {
            time5 = time5.add(BigDecimal.ONE);
        }
        if (nmmordaEntity.getTime6()) {
            time6 = time6.add(BigDecimal.ONE);
        }
        if (nmmordaEntity.getTime7()) {
            time7 = time7.add(BigDecimal.ONE);
        }
        if (nmmordaEntity.getTime8()) {
            time8 = time8.add(BigDecimal.ONE);
        }
        if (nmmordaEntity.getTime9()) {
            time9 = time9.add(BigDecimal.ONE);
        }
        if (nmmordaEntity.getTime10()) {
            time10 = time10.add(BigDecimal.ONE);
        }
        if (nmmordaEntity.getTime11()) {
            time11 = time11.add(BigDecimal.ONE);
        }
        if (nmmordaEntity.getTime12()) {
            time12 = time12.add(BigDecimal.ONE);
        }
        if (nmmordaEntity.getTime13()) {
            time13 = time13.add(BigDecimal.ONE);
        }
        if (nmmordaEntity.getTime14()) {
            time14 = time14.add(BigDecimal.ONE);
        }
        if (nmmordaEntity.getTime15()) {
            time15 = time15.add(BigDecimal.ONE);
        }
        if (nmmordaEntity.getTime16()) {
            time16 = time16.add(BigDecimal.ONE);
        }
        if (nmmordaEntity.getTime17()) {
            time17 = time17.add(BigDecimal.ONE);
        }
        if (nmmordaEntity.getTime18()) {
            time18 = time18.add(BigDecimal.ONE);
        }
        if (nmmordaEntity.getTime19()) {
            time19 = time19.add(BigDecimal.ONE);
        }
        if (nmmordaEntity.getTime20()) {
            time20 = time20.add(BigDecimal.ONE);
        }
        if (nmmordaEntity.getTime21()) {
            time21 = time21.add(BigDecimal.ONE);
        }
        if (nmmordaEntity.getTime22()) {
            time22 = time22.add(BigDecimal.ONE);
        }
        if (nmmordaEntity.getTime23()) {
            time23 = time23.add(BigDecimal.ONE);
        }
        if (nmmordaEntity.getTime24()) {
            time24 = time24.add(BigDecimal.ONE);
        }
        BigDecimal timeAll = time1.add(time2).add(time3).add(time4).add(time5).add(time6).add(time7).add(time8).add(time9).add(time10).add(time11).add(time12).add(time13).add(time14).add(time15).add(time16).add(time17).add(time18).add(time19).add(time20).add(time21).add(time22).add(time23).add(time24);

        nmmordaEntity.setWorkTime(BigDecimal.ZERO);
        nmmordaEntity.setCommon(BigDecimal.ZERO);
        nmmordaEntity.setOverTime(BigDecimal.ZERO);
        nmmordaEntity.setHoliday(BigDecimal.ZERO);

        nmmordaEntity.setLineNum(lineNum + 1);
        for (CalendarsaEntity child : calendarsaEntities) {
            if (child.getNowDate().isEqual(nmmordaEntity.getNowDate())) {
                if ("a".equals(child.getDel())) {
                    if (timeAll.compareTo(BigDecimal.valueOf(8)) >= 0) {
                        nmmordaEntity.setWorkTime(BigDecimal.valueOf(8));
                        nmmordaEntity.setCommon(timeAll.subtract(BigDecimal.valueOf(8)));
                    } else {
                        nmmordaEntity.setWorkTime(timeAll);
                    }
                    /*公休*/
                } else if ("b".equals(child.getDel())) {
                    nmmordaEntity.setOverTime(timeAll);
                    /*节假*/
                } else if ("c".equals(child.getDel())) {
                    nmmordaEntity.setHoliday(timeAll);

                }
                nmmordaEntity.setWeek(child.getWeek());
                nmmordaEntity.setWorkDay(child.getWorkDay());
                nmmordaDao.insert(nmmordaEntity);
                break;
            }
        }


        return ResponseDTO.ok();
    }

    public ResponseDTO<String> update(NmmordaUpdateForm updateForm) {
        NmmordaEntity nmmordaEntity = SmartBeanUtil.copy(updateForm, NmmordaEntity.class);

        List<NmmordaEntity> nmmordaEntities = nmmordaDao.selectList(new LambdaQueryWrapper<NmmordaEntity>().eq(NmmordaEntity::getDocEntry, updateForm.getDocEntry()).eq(NmmordaEntity::getUserId, updateForm.getUserId()).eq(NmmordaEntity::getNowDate, updateForm.getNowDate()).ne(NmmordaEntity::getLineNum, updateForm.getLineNum()));
        if (!nmmordaEntities.isEmpty()) {
            throw new RuntimeException("当前单据的日期重复了！");
        }

        List<CalendarsaEntity> calendarsaEntities = calendarsaDao.selectList(new LambdaQueryWrapper<CalendarsaEntity>().eq(true, CalendarsaEntity::getAbsId, updateForm.getAbsId()));
        NmmordEntity nmmordEntity = nmmordDao.selectById(updateForm.getDocEntry());
        if (!"O".equals(nmmordEntity.getDocStatus())){
            throw new RuntimeException("单据已关闭，禁止修改！");
        }
        if (nmmordEntity.getApproved()){
            throw new RuntimeException("单据已审核，禁止修改！");
        }

        if (calendarsaEntities == null || calendarsaEntities.isEmpty()) {
            throw new RuntimeException("不存在当月日历！");
        }
        BigDecimal time1 = new BigDecimal(0);
        BigDecimal time2 = new BigDecimal(0);
        BigDecimal time3 = new BigDecimal(0);
        BigDecimal time4 = new BigDecimal(0);
        BigDecimal time5 = new BigDecimal(0);
        BigDecimal time6 = new BigDecimal(0);
        BigDecimal time7 = new BigDecimal(0);
        BigDecimal time8 = new BigDecimal(0);
        BigDecimal time9 = new BigDecimal(0);
        BigDecimal time10 = new BigDecimal(0);
        BigDecimal time11 = new BigDecimal(0);
        BigDecimal time12 = new BigDecimal(0);
        BigDecimal time13 = new BigDecimal(0);
        BigDecimal time14 = new BigDecimal(0);
        BigDecimal time15 = new BigDecimal(0);
        BigDecimal time16 = new BigDecimal(0);
        BigDecimal time17 = new BigDecimal(0);
        BigDecimal time18 = new BigDecimal(0);
        BigDecimal time19 = new BigDecimal(0);
        BigDecimal time20 = new BigDecimal(0);
        BigDecimal time21 = new BigDecimal(0);
        BigDecimal time22 = new BigDecimal(0);
        BigDecimal time23 = new BigDecimal(0);
        BigDecimal time24 = new BigDecimal(0);
        if (nmmordaEntity.getTime1()) {
            time1 = time1.add(BigDecimal.ONE);
        }
        if (nmmordaEntity.getTime2()) {
            time2 = time2.add(BigDecimal.ONE);
        }
        if (nmmordaEntity.getTime3()) {
            time3 = time3.add(BigDecimal.ONE);
        }
        if (nmmordaEntity.getTime4()) {
            time4 = time4.add(BigDecimal.ONE);
        }
        if (nmmordaEntity.getTime5()) {
            time5 = time5.add(BigDecimal.ONE);
        }
        if (nmmordaEntity.getTime6()) {
            time6 = time6.add(BigDecimal.ONE);
        }
        if (nmmordaEntity.getTime7()) {
            time7 = time7.add(BigDecimal.ONE);
        }
        if (nmmordaEntity.getTime8()) {
            time8 = time8.add(BigDecimal.ONE);
        }
        if (nmmordaEntity.getTime9()) {
            time9 = time9.add(BigDecimal.ONE);
        }
        if (nmmordaEntity.getTime10()) {
            time10 = time10.add(BigDecimal.ONE);
        }
        if (nmmordaEntity.getTime11()) {
            time11 = time11.add(BigDecimal.ONE);
        }
        if (nmmordaEntity.getTime12()) {
            time12 = time12.add(BigDecimal.ONE);
        }
        if (nmmordaEntity.getTime13()) {
            time13 = time13.add(BigDecimal.ONE);
        }
        if (nmmordaEntity.getTime14()) {
            time14 = time14.add(BigDecimal.ONE);
        }
        if (nmmordaEntity.getTime15()) {
            time15 = time15.add(BigDecimal.ONE);
        }
        if (nmmordaEntity.getTime16()) {
            time16 = time16.add(BigDecimal.ONE);
        }
        if (nmmordaEntity.getTime17()) {
            time17 = time17.add(BigDecimal.ONE);
        }
        if (nmmordaEntity.getTime18()) {
            time18 = time18.add(BigDecimal.ONE);
        }
        if (nmmordaEntity.getTime19()) {
            time19 = time19.add(BigDecimal.ONE);
        }
        if (nmmordaEntity.getTime20()) {
            time20 = time20.add(BigDecimal.ONE);
        }
        if (nmmordaEntity.getTime21()) {
            time21 = time21.add(BigDecimal.ONE);
        }
        if (nmmordaEntity.getTime22()) {
            time22 = time22.add(BigDecimal.ONE);
        }
        if (nmmordaEntity.getTime23()) {
            time23 = time23.add(BigDecimal.ONE);
        }
        if (nmmordaEntity.getTime24()) {
            time24 = time24.add(BigDecimal.ONE);
        }
        BigDecimal timeAll = time1.add(time2).add(time3).add(time4).add(time5).add(time6).add(time7).add(time8).add(time9).add(time10).add(time11).add(time12).add(time13).add(time14).add(time15).add(time16).add(time17).add(time18).add(time19).add(time20).add(time21).add(time22).add(time23).add(time24);
        nmmordaEntity.setWorkTime(BigDecimal.ZERO);
        nmmordaEntity.setCommon(BigDecimal.ZERO);
        nmmordaEntity.setOverTime(BigDecimal.ZERO);
        nmmordaEntity.setHoliday(BigDecimal.ZERO);

        for (CalendarsaEntity child : calendarsaEntities) {
            if (child.getNowDate().isEqual(nmmordaEntity.getNowDate())) {
                if ("a".equals(child.getDel())) {
                    if (timeAll.compareTo(BigDecimal.valueOf(8)) >= 0) {
                        nmmordaEntity.setWorkTime(BigDecimal.valueOf(8));
                        nmmordaEntity.setCommon(timeAll.subtract(BigDecimal.valueOf(8)));
                    } else {
                        nmmordaEntity.setWorkTime(timeAll);
                    }
                    /*公休*/
                } else if ("b".equals(child.getDel())) {
                    nmmordaEntity.setOverTime(timeAll);
                    /*节假*/
                } else if ("c".equals(child.getDel())) {
                    nmmordaEntity.setHoliday(timeAll);

                }
                nmmordaEntity.setWeek(child.getWeek());
                nmmordaEntity.setWorkDay(child.getWorkDay());
                nmmordaDao.update(nmmordaEntity, new LambdaQueryWrapper<NmmordaEntity>().eq(NmmordaEntity::getDocEntry, updateForm.getDocEntry()).eq(NmmordaEntity::getLineNum, updateForm.getLineNum()));
                break;
            }
        }
        return ResponseDTO.ok();
    }

    public ResponseDTO<String> batchDelete(List<Long> idList) {
        nmmordaDao.deleteBatchIds(idList);
        return ResponseDTO.ok();
    }

    public ResponseDTO<String> delete(Long docEntry) {
        NmmordEntity nmmordEntity = nmmordDao.selectById(docEntry);
        AccountEntity accountEntity = accountDao.selectOne(new LambdaQueryWrapper<AccountEntity>().eq(AccountEntity::getAbsId, nmmordEntity.getAbsId()));
        if (accountEntity != null) {
            return ResponseDTO.error(UserErrorCode.ALREADY_EXIST,"存在未删除的汇总单据！，请先删除汇总单据");
        }

        nmmordaDao.deleteById(docEntry);
        return ResponseDTO.ok();
    }

    public ResponseDTO<String> deleteLine(NmmordaQueryForm queryForm) {
        NmmordEntity nmmordEntity = nmmordDao.selectById(queryForm.getDocEntry());
        AccountEntity accountEntity = accountDao.selectOne(new LambdaQueryWrapper<AccountEntity>().eq(AccountEntity::getAbsId, nmmordEntity.getAbsId()));
        if (accountEntity != null) {
            return ResponseDTO.error(UserErrorCode.ALREADY_EXIST,"存在未删除的汇总单据！，请先删除汇总单据");
        }
        nmmordaDao.delete(new LambdaQueryWrapper<NmmordaEntity>().eq(NmmordaEntity::getDocEntry, queryForm.getDocEntry()).eq(NmmordaEntity::getLineNum, queryForm.getLineNum()));
        return ResponseDTO.ok();
    }

    public List<NmmordaEntity> list(MmordbQueryForm queryForm) {
        return nmmordaDao.selectList(new LambdaQueryWrapper<NmmordaEntity>().eq(NmmordaEntity::getDocEntry, queryForm.getDocEntry()).eq(NmmordaEntity::getUserId, queryForm.getUserId()));


    }



}
